<template>
	<view class="content">
		<view class="inner">
			<view class="head">
				<view class="title">头像</view>
				<view class="change" @tap="chooseImage">
					<view class="head-box">
						<!-- 如果是图片 -->
						<image v-show="headImg" class="img" :src="headImg" mode="aspectFill"  ></image>
					</view>
					<image class="arrow" src="../../static/images/right_arrow.png" mode="aspectFill"   ></image>
				</view>
			</view>
			<form>
				<view class="cu-form-group">
					<view class="title">姓名</view>
					<text>{{nickName}}</text>
				</view>
				<view class="cu-form-group">
					<view class="title">部门</view>
					<text>{{deptName}}</text>
				</view>
				<view class="cu-form-group">
					<view class="title">岗位</view>
					<text>{{postNames}}</text>
				</view>
				<view class="cu-form-group">
					<view class="title">性别</view>
					<picker @change="pickerSex" :value="sex" :range="sexPicker">
						<view class="picker">
							{{sex>-1?sexPicker[sex]:''}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">邮箱</view>
					<input placeholder="请输入邮箱" v-model="email" name="input"></input>
				</view>
				
				<view class="cu-form-group">
					<view class="title">手机</view>
					<input placeholder="请输入手机" v-model="phonenumber" name="input"></input>
				</view>
			
			</form>
			
		</view>
		 <div class="common-btn">
		 	<button class="cu-btn block bg-green margin-tb-sm lg" @click="submit">保存</button>
		 </div>
	</view>
</template>

<script>
	import api from '@/api/api.js';
	export default {
		data() {
			return {
				nickName:"",
				deptName:"",
				postNames:"",
				phonenumber:"",
				email:"",
				sexPicker: ['男', '女','未知'],
				sex: 0, // 性别
				headImg: '../../static/images/my_img.png', // 头像,
				headImgUrl:'',
			}
		},
		onLoad() {
			// 修改顶部导航背景色
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#39b54a',
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
			this.getUserInfo();
		},
		methods: {
			bindUser(data){
				var that=this;
				that.nickName=data.user.nickName;
				that.deptName=data.user.dept.deptName;
				that.posts=data.post;
				that.phonenumber=data.user.phonenumber;
				that.email=data.user.email;
				that.sex=data.user.sex;
				if(data.post!=null&&data.post.length>0){
					var postNameStr="";
					data.post.forEach( (v,i) => {
						postNameStr+=v+",";
					},this);
					that.postNames=postNameStr.substr(0,postNameStr.length-1);
					
				}
				if(!that.utils.isEmpty(data.user.avatar)){
					that.headImg=uni.getStorageSync('requestPath')+data.user.avatar;
					this.headImgUrl=data.user.avatar;
				}
			},
			getUserInfo(){
				var data = {};
				var that=this;
				api.getUserInfo(data).then(
					res => {
						that.bindUser(res);
						
					},
					rej => {}
				);
			},
			// 性别选择
			pickerSex(e) {
				this.sex = e.detail.value
			},
			// 选择照片
			chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						//this.headImg = res.tempFilePaths[0]
						uni.uploadFile({
							url: uni.getStorageSync('requestPath')+'/app/common/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'  // 上传附带参数
							},
							success: (uploadFileRes) => {
								// 根据接口具体返回格式   赋值具体对应url
								this.headImgUrl=JSON.parse(uploadFileRes.data).fileName;
								this.headImg=uni.getStorageSync('requestPath')+this.headImgUrl;
							}
						});
						
					}
				});
			},
			// 预览头像
			viewImage() {
				uni.previewImage({
					urls: this.headImg,
					current: this.headImg
				});
			},
			submit(){
				if (this.utils.isEmpty(this.email)) {
					uni.showToast({
						title: "请输入邮箱",
						icon: 'none',
					});
					return
				}
				if (this.utils.isEmpty(this.phonenumber)) {
					uni.showToast({
						title: "请输入手机号",
						icon: 'none',
					});
					return
				}
				var data = {mobile:this.phonenumber,email:this.email,sex:this.sex.toString(),headImg:this.headImgUrl};
				console.log(data);
				api.setUserInfo(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
								title: '修改成功',
								duration: 2000
							});
							setTimeout(function() {
								uni.navigateBack({
								  success: () => {
								  }
								})
							}, 500);
						}
						else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
							});
						}
						
					},
					rej => {}
				);
				
			}
			
		}
	}
</script>


<style scoped lang="scss">
	.content{
		display: flex;
		flex-direction: column;
		
		.inner{
			background: #fff;
			.head{
				margin: 0upx 20upx;
				border-bottom: 1px solid #f7f7f7;
				padding-bottom: 20upx;
				.title{
					font-size: 30upx;
					color: #333;
					padding: 20upx 0 20upx 25upx;
				}
				.change{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.head-box{
						width: 120upx;
						height: 120upx;
						line-height: 100upx;
						border-radius: 50%;
						border: 2px solid #fff;
						font-size: 28upx;
						color: #fff;
						text-align: center;
						margin-right: 20upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;
						justify-content: center;
						overflow: hidden;
						.img{
							width: 100%;
							height: 100%;
						}
					}
					.arrow{
						width: 20upx;
						height: 34upx;
					}
				}
			}
		}
		 
	}
</style>
